package com.wmyg.antifake.utils;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.*;

import java.io.File;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

public class GeoLiteUtil {
    //private static String BASEPATH = "E:\\city\\GeoLite2-City.mmdb" ;
    private static String BASEPATH = "/mnt/files/path/GeoLite2-City.mmdb";
    public static Map getGeoLite(String userIp) throws Exception{
        //GeoIP2-City 数据库文件
        File database = new File(BASEPATH);
        // 读取数据库内容
        DatabaseReader reader = new DatabaseReader.Builder(database).build();
        InetAddress ipAddress = InetAddress.getByName(userIp);
        // 获取查询结果
        CityResponse response = reader.city(ipAddress);

        // 获取国家信息
      /*  Country country = response.getCountry();
        System.out.println(country.getIsoCode());               // 'CN'
        System.out.println(country.getName());                  // 'China'
        System.out.println(country.getNames().get("zh-CN"));    // '中国'

        // 获取省份
        Subdivision subdivision = response.getMostSpecificSubdivision();
        System.out.println(subdivision.getName());   // 'Guangxi Zhuangzu Zizhiqu'
        System.out.println(subdivision.getIsoCode()); // '45'
        System.out.println(subdivision.getNames().get("zh-CN")); // '广西壮族自治区'

        // 获取城市
        City city = response.getCity();
        System.out.println(city.getName()); // 'Nanning'
        Postal postal = response.getPostal();
        System.out.println(postal.getCode()); // 'null'
        System.out.println(city.getNames().get("zh-CN")); // '南宁'*/
        Location location = response.getLocation();
        return  GetLocationBaiduMap.getLocationByBaiduMap(location.getLongitude().toString(),location.getLatitude().toString());
    }
}
